/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package semiSupervise;

import XMLParsing.PartitionFileCreator;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import jwnl.JWNLDistances;
import org.semanticweb.owl.apibinding.OWLManager;
import org.semanticweb.owl.model.OWLClass;
import org.semanticweb.owl.model.OWLOntology;
import org.semanticweb.owl.model.OWLOntologyCreationException;
import org.semanticweb.owl.model.OWLOntologyManager;

/**
 *
 * @author mida
 */
public class partitionnementDesc {

    ArrayList<Element> Ens;
    ArrayList<Ensemble> EC;
    JWNLDistances d;

    partitionnementDesc() {
        Ens = new ArrayList<Element>();
        EC = new ArrayList<Ensemble>();
        d = new JWNLDistances();
        d.Initialize();
    }

    void remplir_Ens() {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        try {
            String s;
            while (!(s = in.readLine()).equals("")) {
                Ens.add(new Element(s));
            }
        } catch (IOException ex) {
            System.out.println("Problem in I/O: " + ex.getMessage());
        }
    }

    int determiner_groupe_proche(OWLClass entity) {
        double dSim, dMax = 0.0;
        int act = 0;
        for (int i = 0; i < Ens.size(); i++) {
            String s = Ens.get(i).toString();
            dSim = d.computeSimilarity(s, entity.toString());
            System.out.println("computeSimilarity(" + entity.toString() + ", " + s + ")=" + (100 - dSim));
            if (dSim > dMax) {
                act = i;
                dMax = dSim;
            }
        }
        return act;
    }

    void traitement_Desc(String physicalURI) {
        OWLOntologyManager manager = null;
        OWLOntology ontology = null;
        try {
            manager = OWLManager.createOWLOntologyManager();
            URI physicalURI1 = URI.create(physicalURI);
            ontology = manager.loadOntologyFromPhysicalURI(physicalURI1);

            //vérifier si k inferieur ou non aux nombre de classe de l'ontologie
            int k = Ens.size();
            if (ontology.getReferencedClasses().size() <= k) {
                System.out.println("nombre de classes:" + ontology.getReferencedClasses().size() + " < k:" + k);
                return;
            }

            int i = 0;
            while (++i <= k) {
                EC.add(new Ensemble());
            }
            for (OWLClass cls : ontology.getReferencedClasses()) {
                EC.get(determiner_groupe_proche(cls)).add(new Element(cls.toString()));
            }
        } catch (OWLOntologyCreationException e) {
            System.out.println("The ontology could not be created: " + e.getMessage());
        }
    }

    void afficherEC() {
        for (int i = 0; i < EC.size(); i++) {
            System.out.println("EC[" + i + "]= " + EC.get(i).toString());
        }
    }

    boolean contains(ArrayList<Ensemble> Ens, String S) {
        for (int i = 0; i < Ens.size(); i++) {
            if (EC.get(i).contains(S.toLowerCase())) {
                return true;
            }
        }
        return false;
    }

    ArrayList<Integer> determiner_groupes_proches(String s) {
        ArrayList<Integer> groupes = new ArrayList<Integer>();
        double dMin = 0.0, dSim = 0.0;
        for (int i = 0; i < EC.size(); i++) {
            dSim = EC.get(i).distance(s, d);
            System.out.println((i + 1) + ") " + (100 - dSim));
            if (dMin < dSim) {
                dMin = dSim;
                groupes.clear();
                groupes.add(i + 1);
            } else if (dMin == dSim) {
                groupes.add(i + 1);
            }
        }
        return groupes;
    }

    ArrayList<Integer> findGroup(String path, String s) {
        if (contains(EC, s)) {
            System.out.println(s + " contenu dans Ens");
            return null;
        }

        afficherEC();
        return (ArrayList) determiner_groupes_proches(s);
    }

    void partitionDesc(String uri) {
        remplir_Ens();
        traitement_Desc(uri);
        afficherEC();
        PartitionFileCreator.createXML(EC);
    }

    public static void main(String[] args) {
        partitionnementDesc t = new partitionnementDesc();
        //t.partitionMoy("file:/C:/Users/mida/Desktop/Example/generations.owl",5);
        t.partitionDesc("file:/C:/Users/mida/Desktop/Example/Wildlife.owl");
        System.out.println("\n\nFinding groupe");
        System.out.println(t.findGroup("file:/C:/Users/mida/Desktop/Example/Wildlife.owl", "cat"));
    }
}
